package com.vmove.childtrain.fragment;

import com.vmove.childtrain.common.BaseFragment;

import java.util.HashMap;
import java.util.Map;

import androidx.fragment.app.Fragment;

public class FragmentFactory {
	public static final int TAB_0 = 0;
	public static final int TAB_1 = 1;
	public static final int TAB_2 = 2;

	/** 记录所有的fragment，防止重复创建 */
//	private static Map<Integer, BaseFragment> mFragmentMap = new HashMap<Integer, BaseFragment>();
	private static Map<Integer, Fragment> mFragmentMap = new HashMap<Integer, Fragment>();

	/** 采用工厂类进行创建Fragment，便于扩展，已经创建的Fragment不再创建 */
	public static Fragment createFragment(int index) {
		Fragment fragment = mFragmentMap.get(index);
		if (fragment == null) {
			switch (index) {
				case TAB_0:
					fragment = new FirstFragment();
					break;
				case TAB_1:
					fragment = new SecondFragment();
					break;
				case TAB_2:
					fragment = new ThirdFragment();
					break;
				default:
					break;
			}
			mFragmentMap.put(index, fragment);
		}
		return fragment;
	}

	public static BaseFragment getFragment(int index){
		return (BaseFragment)mFragmentMap.get(index);
	}
}
